梦入琼楼寒有月,行过石树冻无烟

Node.js console at debug

console API

log\error\info\warn

Node 主要提供了四种常用的 API,其中主要分为 console.logconsole.errorconsole.infoconsole.warn 等/14.Node.js%20console%20at%20debug.html)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
log: hey
log: hey world
log: hey world

error: hey
error: hey error
error: hey error

info: hey
info: world
info: world

warn: hey
warn: hey error
warn: hey error
*/
console.log('log: hey')
console.log('log: hey', 'world')
console.log('log: hey %s', 'world')

console.error('error: hey')
console.error('error: hey', 'error')
console.error('error: hey %s', 'error')

console.info('info: hey')
console.info('info: world')
console.info('info: %s','world')

console.warn('warn: hey')
console.warn('warn: hey', 'error')
console.warn('warn: hey %s', 'error')

在这 console.log 同等于 console.errorconsole.infoconsole.warn 等 API,当然可根据不同的场景和语法结构使用响应的方法。

console.time at timeEnd

通常 console.timeconsole.timeEnd 将用于评估 timetimeEnd 两点之间的时间差(单位为:毫秒)

1
2
3
// timeLabel: 0.069ms
console.time('timeLabel')
console.timeEnd('timeLabel')

console.assert

该 API 方法主要使用其 console.assert 进行断言(assertion),而断言主要是一种放在程序中的一阶逻辑,也就是结果为真或假的逻辑判断式。目的是为了表示与验证程序预期的结果,当程序运行到断言位置时,对应断言应为 true,假设程序终止并给出错误信息。

1
2
3
4
5
6
/*
Assertion failed: start wrong
*/

console.assert(true,'start','wrong')
console.assert(false,'start','wrong')

或者直接通过异常进行处理:

1
2
3
4
5
6
// Assertion failed: error
try {
console.assert(false,'error')
} catch (e) {
console.log(e.message)
}

console.trace

这主要用于将字符串打印到 stderr(标准错误)中,然后以 util.format() 格式的消息和代码到当前的位置堆栈跟踪

1
2
3
4
5
6
7
8
9
10
11
/*
Trace: This will output one end of the error stack
at Object.<anonymous> (/home/kunlun/Development/Web/Demo/node/app.js:1:9)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47
*/
console.trace('This will output one end of the error stack')

console.dir

该方法主要用于深层次的打印,其中可以通过 depth 进行深层次的打印层级。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var obj = {
data: {
main: {
info: {
hey: 'hey.dir'
}
}
}
}

/*
{ data: { main: { info: [Object] } } }
{
data: { main: { info: { hey: 'hey.dir' } } }
}
*/
console.dir(obj)
console.dir(obj, {depth:3})

自定义 stdout

自定义 stdocut 即标准输出,基于 console.Console 将数据输出到指定的文件中,当然这其中会使用到 fs 模块。

1
2
3
4
5
const fs = require('fs')
const file = fs.createWriteStream('desfile.txt')

const logger = new console.Console(file, file);
logger.log('hello,world')

当运行之后,通过 logger.log 的内容数据将会直接写入到 fs.createWriteStream 方法中的 path 参数路径中。

desfile.txt:“hello,world”

debug

对于日志的调试,可以通过 console.log 输出调试日志,由于 console.log 可以支持服务端以及浏览器中使用,且非常的简洁易用,因此这种方法对日志的调试很好。

在开发中由于需要调试就需要多个 console.log 进行输出,之后上传到服务端中又注释 console.log,因此这就是一个加注释和去注释的过程。

但由于上述方法太过于低效,因此 debug 库就可以实现控制输出的调试,他主要的作用就是判断 DEBUG 环境变量,来调整程序的运行环境即可控制日志的是否输出,但他最为核心的还是对 DEBUG 环境变量进行解析,允许开发者选性的控制输出日志。

project

app.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
var debug = require('debug')('http')
, http = require('http')

/*
$ DEBUG=worker:* node app.js
worker:a This is worka +0ms
worker:b This is workb +0ms
worker:a This is worka +239ms
worker:a This is worka +40ms
$ DEBUG=worker:a node app.js
worker:a This is worka +0ms
worker:a This is worka +260ms
worker:a This is worka +42ms
$ DEBUG=worker:b node app.js
worker:b This is workb +0ms
worker:b This is workb +1s
worker:b This is workb +976ms
*/
http.createServer(function(req, res){
debug(req.method + ' ' + req.url);
res.end('hello\n');
}).listen(3000, function(){
debug('listening');
});

// 引入 console.log 工厂
require('./worker');

worker.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var a = require('debug')('worker:a')
, b = require('debug')('worker:b');

function work() {
a('doing lots of uninteresting work');
setTimeout(work, Math.random() * 1000);
}

work();

function workb() {
b('doing some work');
setTimeout(workb, Math.random() * 2000);
}

workb();

条件的定义

有些方法和事件可以通过 --conditions flag 进行使用,但同样可以在 debug 下进行引入参数,来实现对生产环境日志开启条件,在本地环境下开启日志:

Id Name Info
1 browser(浏览器) 任何环境,实现了 Web 浏览器中 JavaScript 所提供的全局浏览器 API
2 development(开发环境/本地环境) 可用于开发环境入口点,必须始终不与生产环境互斥
3 production(生产环境) 用于定义生产环经的切入点,并与本地/开发环境互斥
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const _ = require('lodash')
const debug = require('debug')
const debugA = debug('A:')
const debugB = debug('B:')

/*
$ DEBUG=A: NODE_ENV=production node app.js
A: hey,world! +0ms
$ DEBUG=B: NODE_ENV=production node app.js
B: I am new to debug +0ms
*/
if (process.env.NODE_ENV === 'production') {
debugA.enable = false
}

debugA('hey,world!')
debugB('I am new to debug')

命名空间

Node debug 支持对日志进行分类打印以及命名空间及通佩符,在下文中 DEBUG=ap* 表示将所有以 ap 为开头的调试日志都将会被打印

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const debug = require('debug')
var appDebug = debug('app')
var apiDebug = debug('api')

/*
$ DEBUG=api node app.js
api hey,ApiDebug +0ms
$ DEBUG=app node app.js
app hey,AppDebug +0ms
$ DEBUG=app,api node app.js
app hey,AppDebug +0ms
api hey,ApiDebug +0ms
$ DEBUG=ap* node app.js
app hey,AppDebug +0ms
api hey,ApiDebug +0ms
*/
appDebug('hey,AppDebug')
apiDebug('hey,ApiDebug')
⬅️ Go back